<?php
	include 'config.php';
	include 'includes/user.class.php';
	if(CheckLogin($mysql)){ echo '<meta HTTP-EQUIV="REFRESH" content="0; url=index.php">'; die(); }
	$isloggedin = false; $content = "";
	$perms = CheckPermissions($mysql);
	
	if(!empty($_GET['do'])){
		if($_GET['do'] == "register"){
			if(!empty($_POST['username']) && !empty($_POST['password']) && !empty($_POST['email'])){
				$username = mysql_escape_string($_POST['username']);
				$password = sha1(mysql_escape_string($_POST['password']));
				$email = mysql_escape_string($_POST['email']);
				switch(DoRegister($mysql,$username,$password,$email)){
					case "alreadyregged": $warning = "You seem to be already registered."; break;
					case "emailinuse": $warning = "Email is in use by another user."; break;
					case "success": $content = '<div class="loginbox"><center><h3>Please check your email!</h3></center></div>'; break;
				}
			}
		}elseif($_GET['do'] == "confirm"){
			$code = mysql_escape_string($_GET['code']);
			if(DoRegisterConfirm($mysql,$code)){
				$content = '<meta HTTP-EQUIV="REFRESH" content="5; url=index.php"><div class="loginbox"><center><h3>Your account has been confirmed, please login.</h3></center></div>';
			}else{	$content = '<div class="loginbox"><center><h3>There has been an error, please check the link is correct and support.</h3></center></div>';	}
		}
	}
	
	ob_start();
	
?>

<div class="loginbox">
	<?php if(!empty($warning)){ echo "<div class=\"warningmsg\">$warning</div>"; } ?>
	<form action="register.php?do=register" method="post">
		<center><h3>Register</h3></center>
		<table align="center">
			<tr><td>Username: </td><td><input autocomplete="no" type="text" name="username" /></td></tr>
			<tr><td>Password: </td><td><input type="password" name="password" /></td></tr>
			<tr><td>Confirm Password: </td><td><input type="password" name="passwordc" /></td></tr>
			<tr><td>Email: </td><td><input type="text" name="email" /></td></tr>
			<tr><td>Confirm Email: </td><td><input type="text" name="emailc" /></td></tr>
			<tr><td colspan="2" align="right"><input type="submit" value="Register" /></td></tr>
		</table>
	</form>
</div><br />

<?php
		$sectiontitle = "<a href=\"register.php\">Registration</a>";
		$pagetitle = "Registration";
		if(empty($content)){
			$content = ob_get_flush(); ob_end_clean();
		}else{ ob_end_clean(); }
		include $user_style.'index.php';
?>